from pocketflow import AsyncFlow
from typing import Optional, Dict

from .microflow import (
    PrepareAction,
    PlanAction,
    MCPAction,
    AnswerAction,
    Done
)
from scorpio.core.common import get_logger  
logger = get_logger(__name__)

default_max_iteration=10

class DummyAgent:
    shared: Dict
    _async_flow: Optional[AsyncFlow] = None
    def __init__(self, shared: Dict, max_iteration: int=None):
        self.name="dummy"
        shared["context"]["max_iteration"]=max_iteration or default_max_iteration
        self.shared = shared
        self.prepare=PrepareAction()
        self.plan = PlanAction()
        self.mcp = MCPAction()
        self.answer = AnswerAction()
        self.done = Done()

    def create_flow(self)->AsyncFlow:
       
        
        self.prepare - "prepared" >> self.plan

        self.plan - "limit" >> self.answer
        self.plan - "zero" >> self.done
        self.plan - "single" >> self.mcp
        # to-do: support LLM response multiple tools
        # self.plan - "multiple" >> self.mcp
        self.mcp - "success" >> self.plan
        self.mcp - "failure" >> self.answer # type: ignore
        # if plan node return done, it means end ...
        # plan - "done" >> output
        
        # Create and return the flow, starting with the DecideAction node
        self._async_flow=AsyncFlow(start=self.prepare)
        return self._async_flow
    
    async def run(self):
        logger.info("start to run flow: DummyFlow")
        return await self._async_flow.run_async(self.shared)
